﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BaseData;
using WaterRevenueMS.Core.Repository.BaseData;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Domain.ViewModels.BaseData;
using WaterRevenueMS.Componnent.Entities.BaseData;

namespace WaterRevenueMS.Core.Service.BasicSet.Impl
{
    [Export(typeof(IMeterReaderService))]
    public class MeterReaderService : CoreServiceBase, IMeterReaderService
    {
        [Import]
        public IMeterReaderRepository MeterReaderRepository { get; set; }

        public IQueryable<Componnent.Entities.BaseData.MeterReader> MeterReaders
        {
            get { return MeterReaderRepository.Entities; }
        }

        public Componnent.Common.OperationResult Insert(Domain.ViewModels.BaseData.MeterReader.MeterReaderModel model)
        {
            var entity = new MeterReader
            {

                Dan_WSZ_No = model.CompanyId != 0 ? model.CompanyId : null,

                Chao_BYMC = model.Chao_BYMC,
                Chao_BYDM = model.Chao_BYDM,
                Dian_H = model.Dian_H,
                CreateDT = DateTime.Now,
                ReviseDT=DateTime.Now 

            };
            MeterReaderRepository.Insert(entity);
            return new OperationResult(OperationResultType.Success, "添加成功");
        }

        public Componnent.Common.OperationResult Update(Domain.ViewModels.BaseData.MeterReader.MeterReaderModel model)
        {
            var entity = new MeterReader
            {
                Id=model.id,
                Dan_WSZ_No = model.CompanyId != 0 ? model.CompanyId : null,
                OrderNo = model.OrderNo,
                Chao_BYMC = model.Chao_BYMC,
                Chao_BYDM = model.Chao_BYDM,
                Dian_H = model.Dian_H,
                ReviseDT = DateTime.Now

            };
            MeterReaderRepository.Update(entity);
            return new OperationResult(OperationResultType.Success, "更新成功");
        }

        public Componnent.Common.OperationResult Delete(int Id)
        {
            var model = MeterReaders.FirstOrDefault(t => t.Id == Id);
            model.IsDeleted = true;

            MeterReaderRepository.Delete(model);
            return new OperationResult(OperationResultType.Success, "删除成功");
        }
    }
}
